
# Copyright 2009 Mithun Chaubey 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you 
# may not use this file except in compliance with the License. You may 
# obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0
#     
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing  permissions and
# limitations under the License.

''' Main module for The Living History'''

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext.webapp import template

import lh_user
import timeline

import os

import timeline_handlers
import admin_handlers

class MainPage(webapp.RequestHandler):
    ''' Class for the index page of the site '''
    def get(self):
        ''' Display index page using index.html'''

        timelines = timeline.timeline.all()
        timeline_list = []
        for tl in timelines:
            timeline_list.append(tl)            

        template_values = {
            'timeline_list': timeline_list,
            'current_user': lh_user.get_current_user(self)
           }

        path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
        self.response.out.write(template.render(path, template_values))
        

application = webapp.WSGIApplication(
    [
     ('/',MainPage),
     ('/testcode', testcode.testcode),
     (r'/timelineXML/(.*)', timeline_handlers.simile_timelineXML),
     (r'/timeline/(.*)/compare(.*)', timeline_handlers.compareTimelineRevs),
     (r'/timeline/(.*)/rev/(.*)', timeline_handlers.showTimeline),
     (r'/timeline/(.*)/edit', timeline_handlers.editTimeline),
     (r'/timeline/(.*)/delete', timeline_handlers.deleteTimeline),
     (r'/timeline/(.*)/revisions', timeline_handlers.showTimelineRevisions),
     (r'/timeline/(.*)/createTimelineEvent', timeline_handlers.createTimelineEvent),
     (r'/timeline/(.*)/editTimelineEvent/(.*)', timeline_handlers.editTimelineEvent),
     (r'/timeline/(.*)/deleteTimelineEvent/(.*)', timeline_handlers.deleteTimelineEvent),
     (r'/timeline/(.*)', timeline_handlers.showTimeline),
     ('/createTimeline', timeline_handlers.createTimeline),
     ('/admin', admin_handlers.MainPage),
     ('/admin/users', admin_handlers.admin_users),
     ('/admin/addUser', admin_handlers.addUser),
     ('/admin/editUser/(.*)', admin_handlers.editUser),
     ('/admin/groups', admin_handlers.admin_groups),
     ('/admin/addGroup', admin_handlers.addGroup),
     ('/admin/editGroup/(.*)', admin_handlers.editGroup)
    ],
    debug=True)

def main():
    ''' Returns the WSGI application for Google App Engine '''
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
